# 1. phoneme_mapping.py

PHONEME_TO_VISEME_BASE = {
    "M1": ["a_c", "ai_c", "an_c", "ang_c", "ao_c", "ia_c", "ian_c", "iao_c"],
    "M2": ["e_c", "ê_c", "ei_c", "en_c", "eng_c", "er_c"],
    "M3": ["d_c", "t_c", "n_c", "l_c", "g_c", "k_c", "h_c"],
    "M4": ["i_c", "y_c", "j_c", "q_c", "x_c", "r_c", "ie_c", "in_c", "ing_c"],
    "M5": ["o_c", "uo_c", "ou_c", "ong_c", "iong_c"],
    "M6": ["u_c", "ü_c", "w_c", "un_c", "vn_c", "van_c", "ve_c"],
    "M7": ["f_c", "s_c", "sh_c", "z_c", "zh_c", "c_c", "ch_c"],
    "M8": ["m_c", "b_c", "p_c"]
}

DEFAULT_VISEME = "M2"
phoneme_to_viseme = {}

def load_phoneme_mapping(txt_path):
    with open(txt_path, 'r', encoding='utf-8') as f:
        for line in f:
            parts = line.strip().split()
            if len(parts) < 2:
                continue
            phonemes = parts[1:]
            for ph in phonemes:
                matched = False
                for viseme, sounds in PHONEME_TO_VISEME_BASE.items():
                    if ph in sounds:
                        phoneme_to_viseme[ph] = viseme
                        matched = True
                        break
                if not matched:
                    phoneme_to_viseme[ph] = DEFAULT_VISEME